package usergroup

import (
	"context"

	"github.com/gogf/gf/v2/errors/gerror"

	v1 "gitee.com/liangziyisheng/caas-platform-mc/api/usergroup/v1"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/consts"
	"gitee.com/liangziyisheng/caas-platform-mc/internal/service"
)

func (c *ControllerV1) AssignUser(ctx context.Context, req *v1.AssignUserReq) (res *v1.AssignUserRes, err error) {
	tenantCode := ctx.Value(consts.TokenClaims.TenantCode).(string)

	if len(req.Users) == 0 {
		return nil, gerror.NewCode(consts.AtLeastOneUserToGroup)
	}

	out, err := service.UserGroup().AssignUser(ctx, &v1.AssignUserInput{
		TenantCode: tenantCode,
		Id:         req.Id,
		Users:      req.Users,
	})
	if err != nil {
		return nil, err
	}
	return &v1.AssignUserRes{
		Id: out.Id,
	}, nil

}
